package com.coffee.mongodb;

import com.coffee.core.CoffeeQuery;
import org.springframework.data.mongodb.core.query.CriteriaDefinition;

/**
 * @author: songkui
 * @since: 2022/6/23 11:37
 */
public class MochaMongoQuery<T> implements CoffeeQuery<T> {

    protected long total = 0;
    protected long size = 20;
    protected long current = 1;
    protected String orderASC;  //逗号 隔开
    protected String orderDESC;  //多个 逗号隔开

    protected String[] fieldsInclude;  //as form：【xxx as yyy】

    protected String[] fieldsExclude;  //as form：【xxx as yyy】

    protected CriteriaDefinition criteria;

    protected boolean searchCount = true; // 默认： 如果 total 大于 0则不去查询 总数; 【如需修改； setTotal方法 之后】

    protected Join[] joins;
    protected String master;


     public long skip(){
         return this.current < 1 ? this.current : (this.current - 1) * this.size;
     }

     public int limit(){
         return (int)this.size;
     }

     public CoffeeQuery<T> setFieldsExclude(String... excludes){
         this.fieldsExclude = excludes;
         return this;
     }
     public CoffeeQuery<T> setFieldsInclude(String... includes){
         this.fieldsInclude = includes;
         return this;
     }

     public CoffeeQuery<T> setJoins(Join... joins){
         this.joins = joins;
         return this;
     }

    @Override
    public long getTotal() {
        return this.total;
    }

    @Override
    public long getSize() {
        return this.size;
    }

    @Override
    public long getCurrent() {
        return this.current;
    }

    @Override
    public String getOrderASC() {
        return this.orderASC;
    }

    @Override
    public String getOrderDESC() {
        return this.orderDESC;
    }

    public void setCriteria(CriteriaDefinition criteria) {
        this.criteria = criteria;
    }

    public void setSearchCount(boolean searchCount) {
        this.searchCount = searchCount;
    }

    public void setMaster(String master) {
        this.master = master;
    }

    @Override
    public CoffeeQuery<T>  setTotal(long total) {
        this.total = total;
        this.searchCount = this.total > 0 ? false : true;
        return this;
    }

    @Override
    public CoffeeQuery<T>  setSize(long size) {
        this.size = size;
        return this;
    }

    @Override
    public CoffeeQuery<T>  setCurrent(long current) {
        this.current = current > 0 ? current : 1;
        return this;
    }

    @Override
    public CoffeeQuery<T>  setOrderASC(String orderASC) {
        this.orderASC = orderASC;
        return this;
    }

    @Override
    public CoffeeQuery<T>  setOrderDESC(String orderDESC) {
        this.orderDESC = orderDESC;
        return this;
    }


    public static class Join {

        protected String from;  // join collection
        protected String localField; // master
        protected String foreignField;  //salve id
        protected String as;

        protected boolean unwind = true;

        public Join(){}
        public Join(String from, String localField, String foreignField, String as){
            this.from = from;
            this.localField = localField;
            this.foreignField = foreignField;
            this.as = as;
        }

        public Join(String from, String localField, String foreignField, String as, boolean unwind){
            this.from = from;
            this.localField = localField;
            this.foreignField = foreignField;
            this.as = as;
            this.unwind = unwind;
        }

        public static Join of(String from, String localField, String foreignField, String as){
            return of(from, localField, foreignField, as, true);
        }
        public static Join of(String from, String localField, String foreignField, String as, boolean unwind){
            return new Join(from, localField, foreignField, as, unwind);
        }
    }



}
